##
rm(list=ls())
gc()

library(ggplot2)
library(scales)
library(data.table)
library(stringr)
library(dplyr)
library(tibble)
library(tidyr)
library(gt)
library(readr)



files = c('panel-analysis-2008-2012.csv.gz',
          'panel-analysis-2012-2016.csv.gz',
          'panel-analysis-2016-2020.csv.gz')



data = rbindlist(lapply(files, FUN = function(x){
  d=fread(x, select = c(
                        'Party_year1', 'DemSpExpDiff_nohh', 'RepSpExpDiff_nohh', 
                        'Race','Gender','Age_year1',
                        'HomeownerBlockGroup_year1',
                        'WhiteBlockGroup_year1',
                        'HHIncomeBlockGroup_year1'))[,Years:=str_replace(str_replace(x, '.csv.gz', ''),'panel-analysis-','')]
  
  return(d)
}),fill=T)



### make party switching categories
data[DemSpExpDiff_nohh >= (-.05) & DemSpExpDiff_nohh<= (.05),DemTreat:='D:[-0.05, 0.05]']
data[ DemSpExpDiff_nohh<(-.05),DemTreat:='D:<-0.05']
data[ DemSpExpDiff_nohh> (.05),DemTreat:='D:>0.05']

data[RepSpExpDiff_nohh >= (-.05) & RepSpExpDiff_nohh<=(.05),RepTreat:='R:[-0.05, 0.05]']
data[ RepSpExpDiff_nohh<(-.05),RepTreat:='R:<-0.05']
data[ RepSpExpDiff_nohh> (.05),RepTreat:='R:>0.05']




## Make table 

data[,Democrat:=as.numeric(Party_year1=='Democrat',na.rm=T)]
data[,Republican:=as.numeric(Party_year1=='Republican',na.rm=T)]
data[,Party_year1:=NULL]
data[Gender=='',Gender:=NA]
data[,Female:=as.numeric(Gender=='FEMALE')]
data[,Gender:=NULL]
data[,DemSpExpDiff_nohh:=NULL]
data[,RepSpExpDiff_nohh:=NULL]
data[,White:=as.numeric(Race=='White')]
data[,Race:=NULL]
data[Age_year1>120|Age_year1<18,Age_year1:=NA]
gc()

p1=data[Years=='2008-2012']
p2=data[Years=='2012-2016']
p3=data[Years=='2016-2020']


d1=p1[!is.na(DemTreat),list(Age=round(mean(Age_year1,na.rm=T)),
                            Female=mean(Female,na.rm=T),
         Democrat=mean(Democrat,na.rm=T),
         Republican=mean(Republican,na.rm=T),
         White=mean(White,na.rm=T),
         `Block Group White`=mean(WhiteBlockGroup_year1,na.rm=T),
         `Block Group Med. HH Inc.`=round(mean(HHIncomeBlockGroup_year1,na.rm=T)),
         `Block Group Homeowner`=mean(HomeownerBlockGroup_year1,na.rm=T)
         
         
        ),by='DemTreat']

r1=p1[!is.na(RepTreat),list(Age=round(mean(Age_year1,na.rm=T)),
                            Female=mean(Female,na.rm=T),
                            Democrat=mean(Democrat,na.rm=T),
                            Republican=mean(Republican,na.rm=T),
                            White=mean(White,na.rm=T),
                            `Block Group White`=mean(WhiteBlockGroup_year1,na.rm=T),
                            `Block Group Med. HH Inc.`=round(mean(HHIncomeBlockGroup_year1,na.rm=T)),
                            `Block Group Homeowner`=mean(HomeownerBlockGroup_year1,na.rm=T)
                            
                            
                            
),by='RepTreat']


## p2



d2=p2[!is.na(DemTreat),list(Age=round(mean(Age_year1,na.rm=T)),
                            Female=mean(Female,na.rm=T),
                            Democrat=mean(Democrat,na.rm=T),
                            Republican=mean(Republican,na.rm=T),
                            White=mean(White,na.rm=T),
                            `Block Group White`=mean(WhiteBlockGroup_year1,na.rm=T),
                            `Block Group Med. HH Inc.`=round(mean(HHIncomeBlockGroup_year1,na.rm=T)),
                            `Block Group Homeowner`=mean(HomeownerBlockGroup_year1,na.rm=T)
                            
                            
                            
),by='DemTreat']

r2=p2[!is.na(RepTreat),list(Age=round(mean(Age_year1,na.rm=T)),
                            Female=mean(Female,na.rm=T),
                            Democrat=mean(Democrat,na.rm=T),
                            Republican=mean(Republican,na.rm=T),
                            White=mean(White,na.rm=T),
                            `Block Group White`=mean(WhiteBlockGroup_year1,na.rm=T),
                            `Block Group Med. HH Inc.`=round(mean(HHIncomeBlockGroup_year1,na.rm=T)),
                            `Block Group Homeowner`=mean(HomeownerBlockGroup_year1,na.rm=T)
                            
                            
                            
),by='RepTreat']


## p3



d3=p3[!is.na(DemTreat),list(Age=round(mean(Age_year1,na.rm=T)),
                            Female=mean(Female,na.rm=T),
                            Democrat=mean(Democrat,na.rm=T),
                            Republican=mean(Republican,na.rm=T),
                            White=mean(White,na.rm=T),
                            `Block Group White`=mean(WhiteBlockGroup_year1,na.rm=T),
                            `Block Group Med. HH Inc.`=round(mean(HHIncomeBlockGroup_year1,na.rm=T)),
                            `Block Group Homeowner`=mean(HomeownerBlockGroup_year1,na.rm=T)
                            
                            
                            
),by='DemTreat']

r3=p3[!is.na(RepTreat),list(Age=round(mean(Age_year1,na.rm=T)),
                            Female=mean(Female,na.rm=T),
                            Democrat=mean(Democrat,na.rm=T),
                            Republican=mean(Republican,na.rm=T),
                            White=mean(White,na.rm=T),
                            `Block Group White`=mean(WhiteBlockGroup_year1,na.rm=T),
                            `Block Group Med. HH Inc.`=round(mean(HHIncomeBlockGroup_year1,na.rm=T)),
                            `Block Group Homeowner`=mean(HomeownerBlockGroup_year1,na.rm=T)
                            
                            
                            
),by='RepTreat']


###



t = d1 %>%
  as_tibble %>%
  pivot_longer(Age:`Block Group Homeowner`)%>%
  mutate(Sample = '2008-2012')%>%
  pivot_wider(names_from = 'DemTreat')%>%
  select(Sample, Variable=name,`D:<-0.05`,`D:[-0.05, 0.05]`,`D:>0.05`)%>%
  bind_rows(d2 %>%
              as_tibble %>%
              pivot_longer(Age:`Block Group Homeowner`)%>%
              mutate(Sample = '2012-2016')%>%
              pivot_wider(names_from = 'DemTreat')%>%
              select(Sample, Variable=name,`D:<-0.05`,`D:[-0.05, 0.05]`,`D:>0.05`))%>%
  bind_rows(d3 %>%
              as_tibble %>%
              pivot_longer(Age:`Block Group Homeowner`)%>%
              mutate(Sample = '2016-2020')%>%
              pivot_wider(names_from = 'DemTreat')%>%
              select(Sample, Variable=name,`D:<-0.05`,`D:[-0.05, 0.05]`,`D:>0.05`))%>%
  left_join(
    r1 %>%
      as_tibble %>%
      pivot_longer(Age:`Block Group Homeowner`)%>%
      mutate(Sample = '2008-2012')%>%
      pivot_wider(names_from = 'RepTreat')%>%
      select(Sample, Variable=name,`R:<-0.05`,`R:[-0.05, 0.05]`,`R:>0.05`)%>%
      bind_rows(r2 %>%
                  as_tibble %>%
                  pivot_longer(Age:`Block Group Homeowner`)%>%
                  mutate(Sample = '2012-2016')%>%
                  pivot_wider(names_from = 'RepTreat')%>%
                  select(Sample, Variable=name,`R:<-0.05`,`R:[-0.05, 0.05]`,`R:>0.05`))%>%
      bind_rows(r3 %>%
                  as_tibble %>%
                  pivot_longer(Age:`Block Group Homeowner`)%>%
                  mutate(Sample = '2016-2020')%>%
                  pivot_wider(names_from = 'RepTreat')%>%
                  select(Sample, Variable=name,`R:<-0.05`,`R:[-0.05, 0.05]`,`R:>0.05`))
    ,by=c('Sample','Variable'))
  
  
  
out = t %>%
  gt()%>%
  fmt_number(columns = 3:8, decimals = 3)%>%
  cols_label(
    `D:>0.05` = '> 0.05',
    `D:[-0.05, 0.05]` = '[-0.05, 0.05]',
    `D:<-0.05` = '< -0.05',
    `R:>0.05` = '> 0.05',
    `R:[-0.05, 0.05]` = '[-0.05, 0.05]',
    `R:<-0.05` = '< -0.05',
    `Variable` = 'Variable'
  )%>%
  tab_spanner(label = '\\Delta Dem. Exp.', 3:5)%>%
  tab_spanner(label = '\\Delta Rep. Exp.', 6:8)%>%
  
  
  
  
  as_latex()%>%
  as.character()%>%
  str_replace_all('longtable','tabular')

write_file(out, 'tables/TabS7.tex')
  




